using System;
using System.Collections.Generic;
using System.Windows.Media;
namespace System.Windows.Controls
{
	internal sealed class ItemsControlHelper
	{
		private Panel _itemsHost;
		private ScrollViewer _scrollHost;
		private ItemsControl ItemsControl
		{
			get;
			set;
		}
		internal Panel ItemsHost
		{
			get
			{
				if (this._itemsHost == null && this.ItemsControl != null && this.ItemsControl.get_ItemContainerGenerator() != null)
				{
					DependencyObject dependencyObject = this.ItemsControl.get_ItemContainerGenerator().ContainerFromIndex(0);
					if (dependencyObject != null)
					{
						this._itemsHost = (VisualTreeHelper.GetParent(dependencyObject) as Panel);
					}
				}
				return this._itemsHost;
			}
		}
		internal ScrollViewer ScrollHost
		{
			get
			{
				if (this._scrollHost == null)
				{
					Panel itemsHost = this.ItemsHost;
					if (itemsHost != null)
					{
						DependencyObject dependencyObject = itemsHost;
						while (dependencyObject != this.ItemsControl && dependencyObject != null)
						{
							ScrollViewer scrollViewer = dependencyObject as ScrollViewer;
							if (scrollViewer != null)
							{
								this._scrollHost = scrollViewer;
								break;
							}
							dependencyObject = VisualTreeHelper.GetParent(dependencyObject);
						}
					}
				}
				return this._scrollHost;
			}
		}
		internal ItemsControlHelper(ItemsControl control)
		{
			this.ItemsControl = control;
		}
		internal void OnApplyTemplate()
		{
			this._itemsHost = null;
			this._scrollHost = null;
		}
		internal static void PrepareContainerForItemOverride(DependencyObject element, Style parentItemContainerStyle)
		{
			Control control = element as Control;
			if (parentItemContainerStyle != null && control != null && control.get_Style() == null)
			{
				control.SetValue(FrameworkElement.StyleProperty, parentItemContainerStyle);
			}
		}
		internal void UpdateItemContainerStyle(Style itemContainerStyle)
		{
			if (itemContainerStyle == null)
			{
				return;
			}
			Panel itemsHost = this.ItemsHost;
			if (itemsHost != null && itemsHost.get_Children() != null)
			{
				using (IEnumerator<UIElement> enumerator = itemsHost.get_Children().GetEnumerator())
				{
					while (enumerator.MoveNext())
					{
						UIElement current = enumerator.get_Current();
						FrameworkElement frameworkElement = current as FrameworkElement;
						if (frameworkElement.get_Style() == null)
						{
							frameworkElement.set_Style(itemContainerStyle);
						}
					}
				}
				return;
			}
		}
		internal void ScrollIntoView(FrameworkElement element)
		{
			ScrollViewer scrollHost = this.ScrollHost;
			if (scrollHost == null)
			{
				return;
			}
			GeneralTransform generalTransform = null;
			try
			{
				generalTransform = element.TransformToVisual(scrollHost);
			}
			catch (ArgumentException)
			{
				return;
			}
			Rect rect = new Rect(generalTransform.Transform(default(Point)), generalTransform.Transform(new Point(element.get_ActualWidth(), element.get_ActualHeight())));
			double num = scrollHost.get_VerticalOffset();
			double num2 = 0.0;
			double viewportHeight = scrollHost.get_ViewportHeight();
			double bottom = rect.get_Bottom();
			if (viewportHeight < bottom)
			{
				num2 = bottom - viewportHeight;
				num += num2;
			}
			double top = rect.get_Top();
			if (top - num2 < 0.0)
			{
				num -= num2 - top;
			}
			scrollHost.ScrollToVerticalOffset(num);
			double num3 = scrollHost.get_HorizontalOffset();
			double num4 = 0.0;
			double viewportWidth = scrollHost.get_ViewportWidth();
			double right = rect.get_Right();
			if (viewportWidth < right)
			{
				num4 = right - viewportWidth;
				num3 += num4;
			}
			double left = rect.get_Left();
			if (left - num4 < 0.0)
			{
				num3 -= num4 - left;
			}
			scrollHost.ScrollToHorizontalOffset(num3);
		}
	}
}
